$(document).ready(function(){
	var $serviceBaseurl = $("#service_baseurl");
	var baseurl = $serviceBaseurl.attr("data-url").trim();
	var update_password = $serviceBaseurl.attr("data-password");
	var $updatePasswordDialog = $("#update-password-dialog");
	var $updatePasswordDialog_form = $updatePasswordDialog.children("form");
	var $updateFullnameDialog = $("#update-fullname-dialog");
	var $updateFullnameDialog_form = $updateFullnameDialog.children("form");
	var $systemFullnameSpan = $("#system-fullname-span");
	var $errorDialog = $("<div></div>").hide().appendTo("body");
	var $accordion = $("#accordion");
	var $tabs = $("#tabs");
	$tabs.tabs({
		tools : [{
			text : '修改密码',
			iconCls : 'icon-key',
			handler : function() {
				$updatePasswordDialog.dialog("open");
				$updatePasswordDialog_form.find('td.title').text('修改登录密码');
			}
		},{
			text : '修改姓名',
			iconCls : 'icon-edit',
			handler : function() {
				$updateFullnameDialog.dialog("open");
				$updateFullnameDialog_form.find("input[name=fullname]").val($systemFullnameSpan.text());
			}
		},{
			text : '退出系统',
			iconCls : 'icon-standby',
			handler : function() {
				$.messager.confirm('系统提示','您是否要退出系统',function(r){
					if(r) {
						window.location = baseurl+'/system_logout';
					}
				});
			}
		}]
	});
	$("ul.navul li", $accordion).each(function(){
		var $this = $(this);
		var url = $this.attr("data-url");
		var type = parseInt($this.attr("data-type"));
		var name = $this.children("span").text();
		$this.hover(function(){
			$this.addClass("over");
		},function(){
			$this.removeClass("over");
		}).click(function(){
			$accordion.find("ul.navul li.selected").removeClass("selected");
			$this.addClass("selected");
			var result = $tabs.tabs("exists", name);
			if(result) {
				$tabs.tabs("select", name);
			} else {
				$tabs.tabs("add", {
					tab_type : type,
					title : name,
					closable : true,
					content : '<iframe src="'+url+'" width="100%" height="99%" border="0" style="border:none;"/>'
				});
			}
		});
	});
	$errorDialog.show().window({
		title : '服务器异常提示',
		width : 650,
		height : 450,
		top : 30,
		closed : true,
		minimizable : false,
		collapsible : false,
		onClose : function() {
			$errorDialog.window("body").html('');
		}
	});
	$.ajaxSetup({
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			if(XMLHttpRequest.readyState==4) {
				if(XMLHttpRequest.status==402 || XMLHttpRequest.status==302) {
					$.messager.alert('系统提示','请重新登录','warning',function(){
						window.location = baseurl+'/system_login';
					});
				} else if(XMLHttpRequest.status==403) {
					$changedpassworddialog.dialog("open");
				} else if(XMLHttpRequest.status==500) {
					$errorDialog.window("open");
					$errorDialog.window("body").html(XMLHttpRequest.responseText);
				} else if(XMLHttpRequest.statusText=='error') {
					$.messager.alert('系统提示','请重新登录','warning',function(){
						window.location = baseurl+'/system_login';
					});
				} 
			} else if(XMLHttpRequest.statusText=='error') {
				$.messager.alert('系统提示','请重新登录','warning',function(){
					window.location = baseurl+'/system_login';
				});
			} 
		}
	});
	$.extend($.fn.datagrid.defaults, {
		onLoadError : function(XMLHttpRequest, textStatus, errorThrown) {
			if(XMLHttpRequest.readyState==4) {
				if(XMLHttpRequest.status==402 || XMLHttpRequest.status==302) {
					$.messager.alert('系统提示','系统超时，请重新登录','warning',function(){
						window.location = baseurl+'/system_login';
					});
				} if(XMLHttpRequest.status==500) {
					$errorDialog.window("open");
					$errorDialog.window("body").html(XMLHttpRequest.responseText);
				} else if(XMLHttpRequest.statusText=='error') {
					$.messager.alert('系统提示','请重新登录','warning',function(){
						window.location = baseurl+'/system_login';
					});
				} 
			} else if(XMLHttpRequest.statusText=='error') {
				$.messager.alert('系统提示','请重新登录','warning',function(){
					window.location = baseurl+'/system_login';
				});
			} 
		}
	});
	$updatePasswordDialog.show().dialog({
		title : '修改登录密码',
		iconCls : 'icon-edit',
		modal : true,
		closed : true,
		width : 400,
		height : 250,
		buttons : [{
			text : '保存',
			iconCls : 'icon-save',
			handler : function() {
				$updatePasswordDialog_form.submit();
			}
		},{
			text : '返回',
			iconCls : 'icon-back',
			handler : function() {
				$updatePasswordDialog.dialog("close");
			}
		}],
		onClose : function() {
			$updatePasswordDialog_form.form("reset");
		}
	});
	$updatePasswordDialog_form.form({
		success : function(data) {
			var obj = $.parseJSON(data);
			if(obj.success) {
				$updatePasswordDialog.dialog("close");
				$.messager.alert('系统提示','您已成功修改密码,请重新登录','info',function(){
					window.location = baseurl+'/system_logout';
				});
			} else {
				$.messager.alert('系统提示',obj.message,'warning');
			}
		},
		onLoadError : function(error) {
			$.messager.alert('info',error,'warning');
		}
	});
	$updateFullnameDialog.show().dialog({
		title : '修改姓名',
		iconCls : 'icon-edit',
		modal : true,
		closed : true,
		width : 450,
		height : 180,
		buttons : [{
			text : '保存',
			iconCls : 'icon-save',
			handler : function() {
				$updateFullnameDialog_form.submit();
			}
		},{
			text : '返回',
			iconCls : 'icon-back',
			handler : function() {
				$updateFullnameDialog.dialog("close");
			}
		}],
		onClose : function() {
			$updateFullnameDialog_form.form("reset");
		}
	});
	$updateFullnameDialog_form.form({
		success : function(data) {
			var obj = $.parseJSON(data);
			if(obj.success) {
				var fullname = $updateFullnameDialog_form.find("input[name=fullname]").val();
				$systemFullnameSpan.text(fullname);
				$updateFullnameDialog.dialog("close");
			} else {
				$.messager.alert('系统提示',obj.message,'warning');
			}
		},
		onLoadError : function(error) {
			$.messager.alert('info',error,'warning');
		}
	});
	if(update_password==='true') {
		$updatePasswordDialog.dialog("open");
		$updatePasswordDialog_form.find('td.title').text('您的密码是初始密码，请尽快修改！');
	}
});